#include "stationwidget.h"
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QToolBox>
#include <QIcon>
#include <QDebug>
#include <QPair>
#include <QFont>
#include <QScrollBar>
#include "common.h"
#include "editwidget.h"

StationWidget::StationWidget(QScrollArea *parent)
    :QScrollArea(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);

    setMaximumSize((Screen::GetScreenWidth())>>1, (Screen::GetScreenHeight()));
    setMinimumSize((Screen::GetScreenWidth())>>5, (Screen::GetScreenHeight())>>4);
//    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
    setFrameShape(QScrollArea::NoFrame);
    setWidgetResizable(true);

    tree_station = new QTreeWidget(this);
    tree_station->setAlternatingRowColors(true);
    tree_station->setPalette(QPalette(QColor(210,210,210)));
    tree_station->setFrameShape(QTreeWidget::NoFrame);
    tree_station->setFixedHeight(Screen::GetScreenHeight()>>1);
    tree_station->setStyleSheet("QTreeWidget{background: transparent; border: none;}");//transparent
    tree_station->setSelectionMode(QAbstractItemView::SingleSelection);
    tree_station->setAcceptDrops(true);
    tree_station->setDragEnabled(true);
    tree_station->setDropIndicatorShown(true);
    tree_station->setDragDropMode(QAbstractItemView::InternalMove);
    tree_station->setHeaderHidden(true);
    tree_station->horizontalScrollBar()->hide();
    tree_station->verticalScrollBar()->hide();

    setWidget(tree_station);

    connect(tree_station, &QTreeWidget::itemClicked, this, &StationWidget::clickShow);
}

StationWidget::~StationWidget()
{

}

void StationWidget::expandAll()
{
    this->tree_station->expandAll();
}

QString StationWidget::GetCurrentXml()
{
    return tree_station->currentItem()->text(tree_station->currentColumn());
}


void StationWidget::clickShow(QTreeWidgetItem *item, int column)
{
    LOG_QDEBUG<<__func__<<column<<item->text(column);
    if (item->text(column).contains("xml"))
    {
        if (myapp::mDynamicMapParam->mMapObjectShow[item->text(column)] == NULL)
        {
            myapp::mDynamicMapParam->mMapObjectShow[item->text(column)] = new EditWidget(item->text(column), QSize(myapp::mDynamicMapParam->m_width, myapp::mDynamicMapParam->m_height));
        }
        emit signal_select_file(item->text(column));
    }
}

void StationWidget::GenerateTree()
{
    QString file_name;
    QStringList list;
    tree_station->clear();

    QTreeWidgetItem *root = new QTreeWidgetItem;
    root->setText(0, myapp::mDynamicMapParam->m_project_name);
    tree_station->addTopLevelItem(root);
    if (myapp::mDynamicMapParam->m_type == LCD)
    {
        for (auto it = myapp::mDynamicMapParam->mMapObjectShow.begin(); it != myapp::mDynamicMapParam->mMapObjectShow.end(); it++)
        {
            if (ZERO != it.key().right(3).compare("xml"))
                continue;
            file_name = it.key();
            QTreeWidgetItem * item = new QTreeWidgetItem;
            item->setText(0, it.key());
            root->addChild(item);
        }
    }
    else if (myapp::mDynamicMapParam->m_type == DRM)
    {
        QStringList othersList;
        QStringList allList;
        QStringList intervalList;
        QStringList arriveList;
        for (auto it = myapp::mDynamicMapParam->mMapObjectShow.begin(); it != myapp::mDynamicMapParam->mMapObjectShow.end(); it++)
        {
            QString fileNameAndSuffix = it.key();
            if (ZERO != fileNameAndSuffix.right(3).compare("xml"))
                continue;
            file_name = fileNameAndSuffix.left(fileNameAndSuffix.size() - 4);

            list = file_name.split("_");
            QString uiType = list.at(ParamType);
            if (ZERO == uiType.compare(AllFlag)) {
                allList.append(fileNameAndSuffix);
            } else if (ZERO == uiType.compare(IntervalFlag)) {
                intervalList.append(fileNameAndSuffix);
            } else if (ZERO == uiType.compare(ArriveFlag)) {
                arriveList.append(fileNameAndSuffix);
            } else {
                othersList.append(fileNameAndSuffix);
            }
        }

        foreach (QString name, othersList) {
            QTreeWidgetItem *item = new QTreeWidgetItem(QStringList() << name);
            root->addChild(item);
        }
        foreach (QString name, allList) {
            QTreeWidgetItem *item = new QTreeWidgetItem(QStringList() << name);
            root->addChild(item);
        }
        foreach (QString name, intervalList) {
            QTreeWidgetItem *item = new QTreeWidgetItem(QStringList() << name);
            root->addChild(item);
        }
        foreach (QString name, arriveList) {
            QTreeWidgetItem *item = new QTreeWidgetItem(QStringList() << name);
            root->addChild(item);
        }
    }
}
